<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Face3对象定义Geometry的三角形面</title>
    <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
    <link rel="stylesheet" href="common.css">
</head>

<body>

</body>
<script>
    // 【第一步：创建模型】
    var scene = new THREE.Scene(); //  创建场景对象Scene
    var geometry = new THREE.Geometry(); //声明一个几何体对象Geometry
    // 【点】
    var p1 = new THREE.Vector3(0, 0, 0); //顶点1坐标
    var p2 = new THREE.Vector3(0, 80, 0); //顶点2坐标
    var p3 = new THREE.Vector3(200, 0, 0); //顶点3坐标
    var p4 = new THREE.Vector3(0, 0, 100); //顶点4坐标
    geometry.vertices.push(p1, p2, p3, p4); //顶点坐标添加到geometry对象
    // 【面】Face3取得3个点来源于geometry.vertices，就是[p1,p2,p3,p4]
    var face1 = new THREE.Face3(0, 1, 2);
    var face2 = new THREE.Face3(0, 1, 3);
    // 【法线】
    face1.normal = new THREE.Vector3(0, 0, -1);
    face2.normal = new THREE.Vector3(1, 0, 0);
    // 【集成】
    geometry.faces.push(face1, face2);
    // 【设置属性颜色，单色和渐变色】
    face1.color = new THREE.Color(0xff00ff);
    face2.vertexColors = [
        new THREE.Color(0xffff00),
        new THREE.Color(0xff00ff),
        new THREE.Color(0x00ffff),
    ]

    //材质对象
    var material = new THREE.MeshLambertMaterial({
        vertexColors: THREE.VertexColors, //以顶点颜色为准
        side: THREE.DoubleSide, //两面可见
    });
    var axisHelper = new THREE.AxisHelper(300); // 辅助坐标系
    scene.add(axisHelper); //场景中加入辅助线
    var mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh（模型，材质）
    scene.add(mesh); //将模型添加到场景中


    // 【第2步：光源设置】
    var point = new THREE.PointLight(0xffffff); //设置点光源
    point.position.set(400, 200, 300); //点光源位置
    scene.add(point); //点光源添加到场景中
    var ambient = new THREE.AmbientLight(0x444444, 0.1); //可以添加环境光(颜色，强度)
    scene.add(ambient);

    // 【第三步：相机设置】
    var width = window.innerWidth; //窗口宽度
    var height = window.innerHeight; //窗口高度
    var k = width / height; //窗口宽高比
    var s = 300; //三维场景显示范围控制系数，系数越大，显示的范围越大
    var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000); //创建相机对象
    camera.position.set(200, 300, 200); //设置相机位置
    camera.lookAt(scene.position); //设置相机方向(指向的场景对象)

    // 【第四步：渲染对象】
    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height); //设置渲染区域尺寸
    renderer.setClearColor(0xb9d3ff, 0.3); //设置背景颜色
    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象
    renderer.render(scene, camera); //执行渲染操作（场景、相机）
</script>

</html>